package com.itboye.hutouben.activity.qianbao;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.bean.BankCodeBean;
import com.itboye.hutouben.bean.MoneyShengyuBean;
import com.itboye.hutouben.presenter.SystemMessgeAPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 我的钱包
 */
public class MyQianBaoActivity extends BaseActivity implements Observer {
    TextView add_shap_title_tv, save;
    ImageView close_icon;
    LinearLayout qianbao_main;
    TextView qianbao_line;
    TextView qianbao_money, qianbao_zhanghu;
    RelativeLayout qianbao_chongzhi, qianbao_tixian, qianbao_zhangdan, qianbao_yinhangka;

    RelativeLayout layout;
    PopupWindow mPopWin;
    SystemMessgeAPresenter presenter;
    MoneyShengyuBean bean;
    List<BankCodeBean> typebean;
    RelativeLayout titlebar;
    private Dialog dialog;// 自定义Dialog
    EditText dialog_et;
    private String typeId;//账号绑定ID

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ny_qian_bao_activity);
        add_shap_title_tv.setText(R.string.my_qianbao);
        save.setVisibility(View.VISIBLE);
        save.setText(R.string.set_up_pass);
        presenter = new SystemMessgeAPresenter(this);
        typebean = new ArrayList<BankCodeBean>();
        presenter.onBangDinType();
        bean = new MoneyShengyuBean();
        showExtraDialog();

    }

    @Override
    protected void onStart() {
        super.onStart();
        presenter.onMoney(IsUtilUid.isUid());
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.save:// 设置支付密码
                Intent intents = new Intent(MyApplcation.ctx, ZhiFuPassActivity.class);
                startActivity(intents);
                break;
            case R.id.qianbao_chongzhi://充值
                intent = new Intent(this, ChongZhiActivity.class);
                startActivity(intent);
                break;
            case R.id.qianbao_tixian://提现
                dialog.show();
                break;
            case R.id.qianbao_zhangdan://账单
                intent = new Intent(this, JiaoYiJiLuActivity.class);
                startActivity(intent);
                break;
            case R.id.qianbao_yinhangka://绑定银行卡
                intent = new Intent(this, MyZhangHuActivity.class);
                intent.putExtra("type","qianbao");
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    /**
     * 自定义Dialog
     */
    /**
     * 自定义Dialog
     */
    private void showExtraDialog() {
        dialog = new Dialog(MyQianBaoActivity.this, R.style.rel_dialog);
        View view = LayoutInflater.from(MyQianBaoActivity.this).inflate(
                R.layout.dialog_normal_layout, null);
        dialog.setContentView(view);
        dialog_et = (EditText) view.findViewById(R.id.dialog_et);

        TextView user_title = (TextView) view.findViewById(R.id.user_title);
        user_title.setText("输入您的6位支付密码");
        dialog_et.setText("");

        TextView dialog_ok = (TextView) view.findViewById(R.id.dialog_ok);
        TextView dialog_quxiao = (TextView) view.findViewById(R.id.dialog_quxiao);
        final String rele = (String) SPUtils.get(MyApplcation.ctx, null, Const.ZHIFUMIMA, "");
        dialog.setCanceledOnTouchOutside(false);
        dialog_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();

            }
        });
        dialog_quxiao.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {


                if(dialog_et.getText().toString().equals("")){
                    ByAlert.alert("请输入支付密码");
                }else {
                    presenter.onByWalletCheckSecret(IsUtilUid.isUid(), dialog_et.getText().toString().trim());
                    showProgressDialog("验证中..", true);
                }

            }
        });
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        // 屏幕宽度
        int screenWidth = display.getWidth();
        // 屏幕高度
        int screenHeight = display.getHeight();
        Window window = dialog.getWindow();

        WindowManager.LayoutParams lp = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        lp.width = screenWidth * 3 / 4;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

    }


    /**
     * 提现选择
     */
    private void tixianPop(View v) {
        layout = (RelativeLayout) LayoutInflater.from(this).inflate(
                R.layout.item_tixian_xuanze, null);
        TextView tixian_zhifubao = (TextView) layout.findViewById(R.id.tixian_zhifubao);
        TextView tixian_yinhangka = (TextView) layout.findViewById(R.id.tixian_yinhangka);

        mPopWin = new PopupWindow(layout,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopWin.setBackgroundDrawable(new BitmapDrawable());
        mPopWin.showAsDropDown(qianbao_line, 5, 1);
        mPopWin.update();

        tixian_zhifubao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyQianBaoActivity.this, TiXianToZhiFuBaoActivity.class);
                startActivity(intent);
                mPopWin.dismiss();
            }
        });
        tixian_yinhangka.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyQianBaoActivity.this, TiXianActivity.class);
                startActivity(intent);
                mPopWin.dismiss();
            }
        });
    }

    @Override
    public void update(Observable observable, Object data) {
        try{
            closeProgressDialog();
        }catch (Exception e){}
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == presenter.Yue_success) {
                bean = (MoneyShengyuBean) entity.getData();
                Log.d("byaltitledatae", bean.getTotal());
                qianbao_money.setText("" + Double.parseDouble(bean.getTotal()) / 100 + "元");
                double in = Double.parseDouble(bean.getTotal());
                SPUtils.put(MyQianBaoActivity.this, null,
                        Const.QIAN, in+"");
            }
            if (entity.getEventType() == presenter.BangdingType_success) {
                typebean = (List<BankCodeBean>) entity.getData();
                typeId = typebean.get(0).getId();

                SPUtils.put(MyApplcation.ctx, null,
                        Const.ISZID, typebean.get(0).getId());
                Log.d("tytpID", typeId);

                ;
            }
            if (entity.getEventType() == presenter.PWD_success) {
                Intent intent = new Intent(MyQianBaoActivity.this, TiXianToZhiFuBaoActivity.class);
                intent.putExtra("typeId", typeId);
                startActivity(intent);
                dialog.dismiss();
            }
        }
    }
}
